package com.msw.jcwapi.common.parametor.service;

import com.alibaba.fastjson2.JSON;
import com.msw.jcwapi.common.parametor.model.WbstructPara;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@Service
public class WbstructParaService {
    private static final String JSON_FILE_PATH = "static/wb/wbstruct.json"; // 请根据实际情况调整路径
    public List<WbstructPara> loadParameters() throws IOException {
        // 获取资源的输入流
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(JSON_FILE_PATH);
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {

            if (inputStream == null) {
                throw new IOException("Resource not found: " + JSON_FILE_PATH);
            }

            // 读取输入流内容为字符串
            StringBuilder jsonContentBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                jsonContentBuilder.append(line);
                jsonContentBuilder.append("\n");
            }
            String jsonContent = jsonContentBuilder.toString().trim();

            // 解析JSON内容为List<Parameter>
            return JSON.parseArray(jsonContent, WbstructPara.class);
        }
    }
}
